<template>
  <BasicModal
    v-bind="$attrs"
    :width="modalWidth"
    @register="registerModal"
    :title="getTitle"
    :okText="okText"
  >
    <template #footer>
      <a-button @click="cancel">取消</a-button>
      <a-button type="primary" @click="handleSubmit"> {{ t('sys.configuration.confirm') }} </a-button>
    </template>
    <!-- :labelWidth="100"
      :schemas="schemas"
      :actionColOptions="{ span: 24 }"
      @submit="handleSubmit"
      :showActionButtonGroup="showActionButtonGroup"
      v-if="current === 0" -->
    <BasicForm @register="registerForm"/>
    <!-- :labelWidth="100"
      :schemas="schemasData"
      :actionColOptions="{ span: 24 }"
      :baseColProps="{ span: 24 }"
      @submit="handleSubmit"
      :showActionButtonGroup="showActionButtonGroup"
      v-if="current === 1" -->
  </BasicModal>
</template>
<script lang="ts" setup>
  import { Steps } from 'ant-design-vue';
  import { ref, computed, unref } from 'vue';
  import { GetmerchantSaveApi } from '@/api/demo/businessOwner';
  import { BasicModal, useModalInner } from '@/components/Modal';
  import { BasicForm, useForm } from '@/components/Form';
  import { accountFormSchema } from './account.data';
  import { useI18n } from "@/hooks/web/useI18n";

  const { t } = useI18n();
  defineOptions({ name: 'AccountModal' });
  // const emit = defineEmits(['success', 'register']);
  const isUpdate = ref(true);
  const rowId = ref('');
  const modalWidth = ref('30%'); // 可以是像 600 这样的像素值，或者百分比 '50%'
  const current = ref(0);
  const okText = ref('下一步');
  //注册表格数据
  const [registerForm, { resetFields, setFieldsValue, getFieldsValue }] = useForm({
    labelWidth: 90,
    baseColProps: { span: 24 },
    schemas: accountFormSchema,
    showActionButtonGroup: false,
  });
  //注册弹窗数据
  const [registerModal, { setModalProps, closeModal }] = useModalInner(async (data) => {
    await resetFields();
    setModalProps({ confirmLoading: false });
    isUpdate.value = !!data?.isUpdate;

    if (unref(isUpdate)) {
      rowId.value = data.record.id;
      setFieldsValue({
        ...data.record,
      });
    }
    // const treeData = await getDeptList();
    //更新表单中的schema，只更新函数所传的参数
    // updateSchema([
    //   {
    //     field: 'pwd',
    //     show: !unref(isUpdate),
    //   },
    //   {
    //     field: 'dept',
    //     componentProps: { treeData },
    //   },
    // ]);
  });

  const getTitle = computed(() => (!unref(isUpdate) ? t('sys.configuration.add') : t('sys.configuration.edit')));

  // async function handleSubmit() {
  //   try {
  //     const values = await validate();
  //     setModalProps({ confirmLoading: true });
  //     // TODO custom api
  //     closeModal();
  //     emit('success', { isUpdate: unref(isUpdate), values: { ...values, id: rowId.value } });
  //   } finally {
  //     setModalProps({ confirmLoading: false });
  //   }
  // }
  // 提交
  async function handleSubmit() {
    let paramsValue={...getFieldsValue(),id: rowId.value}
    const Data=await GetmerchantSaveApi(paramsValue)
    closeModal();
  }
  // 取消
  function cancel() {
    closeModal();
  }
</script>
